<?php 
namespace plugin;
class Admin extends \Plugin {
	
	protected $_admin_auth = 1;

	public function index(){
		$configs = \Ebcms::db() -> select('config', '*', [
			'ORDER' => [
				'weight' => 'ASC',
				'id' => 'ASC',
			]
		]);
		$groups = [];
		foreach ($configs as $key => $value) {
			if (!isset($groups[$value['group']])) {
				$groups[$value['group']] = [];
			}
			$groups[$value['group']][] = $value;
		}
		$this -> assign('groups', $groups);
		return \mylib\Response::html($this -> fetch());
	}

	public function add(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> insert('config', [
				'group'=>\mylib\Input::post('group'),
				'title'=>\mylib\Input::post('title'),
				'name'=>\mylib\Input::post('name'),
				'type'=>\mylib\Input::post('type'),
				'weight'=>\mylib\Input::post('weight'),
				'tips'=>\mylib\Input::post('tips'),
			]);
			return $this -> success('操作成功！', plugin_url('/config/admin/index'));
		}else{
			return \mylib\Response::html($this -> fetch());
		}
	}

	public function edit(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> update('config', [
				'group'=>\mylib\Input::post('group'),
				'title'=>\mylib\Input::post('title'),
				'name'=>\mylib\Input::post('name'),
				'type'=>\mylib\Input::post('type'),
				'weight'=>\mylib\Input::post('weight'),
				'tips'=>\mylib\Input::post('tips'),
			], [
				'id'=>\mylib\Input::post('id')
			]);
			return $this -> success('操作成功！', plugin_url('/config/admin/index'));
		}else{
			$config = \Ebcms::db() -> get('config', '*', ['id'=>\mylib\Input::get('id')]);
			$this -> assign('config', $config);
			return \mylib\Response::html($this -> fetch());
		}
	}

	public function del(){
		if (\Ebcms::isAjax()) {
			\Ebcms::db() -> delete('config', [
				'id'=>\mylib\Input::post('id'),
			]);
			return $this -> success('操作成功！');
		}
	}
}